#include <stdio.h>
#include "MenuSelectionElement.h"
#include "GL/glut.h"
#include "GameManager.h"

MenuSelectionElement::MenuSelectionElement(TextureManager * textureManager, char * textureName, 
										   Vector2D position, Vector2D size, 
										   Vector2D * textureVertices, PrintStringList * stringList)
: MenuElement(textureManager, textureName, position, size, textureVertices)
{
	isSelected = false;
	selectedElement = false;
	selectedIndex = -1;
	this->stringList = stringList;
	FindLongestString();
}
MenuSelectionElement::MenuSelectionElement(TextureManager * textureManager, char * textureName, 
										   Vector2D position, Vector2D size, Vector2D * textureVertices, 
										   GameManager * gameManager, void (GameManager::*clickAction)(char*), 
										   PrintStringList * stringList)
: MenuElement(textureManager, textureName, position, size, textureVertices, gameManager, NULL)
{
	isSelected = false;
	selectedElement = false;
	selectedIndex = -1;
	this->stringList = stringList;
	this->clickStringAction = clickAction;
	FindLongestString();
}
MenuSelectionElement::MenuSelectionElement(char * label, TextureManager * textureManager, char * textureName, 
										   Vector2D position, Vector2D size, Vector2D * textureVertices, 
										   GameManager * gameManager, void (GameManager::*clickAction)(char*), 
										   PrintStringList * stringList)
: MenuElement(label, textureManager, textureName, position, size, textureVertices, gameManager, NULL)
{
	isSelected = false;
	selectedElement = false;
	selectedIndex = -1;
	this->stringList = stringList;
	this->clickStringAction = clickAction;
	FindLongestString();
}
MenuSelectionElement::~MenuSelectionElement()
{
	if(stringList)	
		delete(stringList);
	stringList = NULL;
}
void MenuSelectionElement::FindLongestString()
{
	longestStringLength = stringList->GetLongestStringLength();
	
	if(size.x < 2.5)
		size.x = 2.5;
	if(size.y < 5.0)
		size.y = 5.0;
		
	textSize.x = size.x/longestStringLength;
	if(textSize.x > 2)
		textSize.x = 2;
		
	textSize.y = textSize.x * 1.25;
	
	stringList->OrderList(position, textSize.x);
	printf("size.y = %f\n", size.y);
	printf("textSize.x * 1.5 = %f\n", textSize.x*1.5);
	printf("(size.y / (textSize.x * 1.5)) = %f\n", (size.y / (textSize.x * 1.5)));
	printf("(int)(size.y / (textSize.x * 1.5)) = %d\n", (int)(size.y / (textSize.x * 1.5)));
	
	listLines = (int)(size.y / (textSize.x * 1.5)) - 1;
	
	printf("string list length = %d, list lines = %d\n", stringList->GetLength(), listLines);
	if(stringList->GetLength() > listLines + 1)
	{
		printf("scroll needed!\n");
		scrollNeeded = true;
		
		textSize.x = (size.x - 2.5)/longestStringLength;
		
		if(textSize.x > 2)
			textSize.x = 2;
		stringList->OrderList(position, textSize.x);
		textSize.y = textSize.x * 1.25;
	}
	else
		scrollNeeded = false;
	scrollIndex = 0;
}
void MenuSelectionElement::Draw(Vector2D parentPosition)
{
	
	Vector2D vertices[4];
	
	Vector2D drawTextPosition = parentPosition;
	drawTextPosition.y -= scrollIndex * textSize.x * 1.5;
	stringList->Draw(drawTextPosition , scrollIndex, scrollIndex + listLines);
	
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0,100,100,0, -1, 1);
	
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glTranslatef(0, 0, -0.5);
	
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	
	//draw selection highlight
	if(isSelected)
	{
		if(selectedIndex >= scrollIndex && selectedIndex <= scrollIndex + listLines)
		{
			float highlightSize = size.x;
			if(scrollNeeded)
				highlightSize -= 2.5;
				
			vertices[0] = parentPosition + position;
			vertices[0].y += (selectedIndex-scrollIndex) * (textSize.x * 1.5);
			vertices[1] = Vector2D(vertices[0].x + highlightSize, vertices[0].y);
			vertices[2] = Vector2D(vertices[1].x, vertices[1].y + (textSize.x * 1.5));
			vertices[3] = Vector2D(vertices[0].x, vertices[2].y);
			glColor3f(0,1,0);	
			glBegin(GL_QUADS);
			
			for(int i = 0; i < 4; i++)
				glVertex3f(vertices[i].x, vertices[i].y, 1);
			glEnd();
		}
	}
	
	//draw scroll bar
	if(scrollNeeded)
	{
		vertices[0] = parentPosition + position;
		vertices[0].x += size.x - 2.5;
		vertices[1] = Vector2D(vertices[0].x, vertices[0].y + size.y);
		
		glColor3f(0,0,0);
		glBegin(GL_LINES);
		for(int i = 0; i < 2; i++)
			glVertex3f(vertices[i].x, vertices[i].y, 1);
		
		//up arrow
			glVertex3f(vertices[0].x + 1.25, vertices[0].y + 1.0, 1);
			glVertex3f(vertices[0].x + 2.0, vertices[0].y + 1.75, 1);
			
			glVertex3f(vertices[0].x + 2.0, vertices[0].y + 1.75, 1);
			glVertex3f(vertices[0].x + 0.5, vertices[0].y + 1.75, 1);
			
			glVertex3f(vertices[0].x + 0.5, vertices[0].y + 1.75, 1);
			glVertex3f(vertices[0].x + 1.25, vertices[0].y + 1.0, 1);
			
			glVertex3f(vertices[0].x, vertices[0].y + 2.5, 1);
			glVertex3f(vertices[0].x + 2.5, vertices[0].y + 2.5, 1);
			
			
		//down arrow
			glVertex3f(vertices[0].x + 1.25, vertices[1].y - 1.0, 1);
			glVertex3f(vertices[0].x + 2.0, vertices[1].y - 1.75, 1);
			
			glVertex3f(vertices[0].x + 2.0, vertices[1].y - 1.75, 1);
			glVertex3f(vertices[0].x + 0.5, vertices[1].y - 1.75, 1);
			
			glVertex3f(vertices[0].x + 0.5, vertices[1].y - 1.75, 1);
			glVertex3f(vertices[0].x + 1.25, vertices[1].y - 1.0, 1);
			
			glVertex3f(vertices[0].x, vertices[1].y - 2.5, 1);
			glVertex3f(vertices[0].x + 2.5, vertices[1].y - 2.5, 1);
			
		glEnd();
		
		
		
		//down arrow
	}
	
	//draw background
	vertices[0] = parentPosition + position;
	vertices[1] = Vector2D(vertices[0].x + size.x, vertices[0].y);
	vertices[2] = Vector2D(vertices[1].x, vertices[1].y + size.y);
	vertices[3] = Vector2D(vertices[0].x, vertices[2].y);
	
	glColor3f(1,1,1);	
	glBegin(GL_QUADS);

	for(int i = 0; i < 4; i++)
		glVertex3f(vertices[i].x, vertices[i].y, 1);
	glEnd();
	
	
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	
	
}
bool MenuSelectionElement::Click(Vector2D parentPosition, Vector2D mousePosition)
{
	if(!gameManager)
		return false;
		
	mousePosition.y = 100 - mousePosition.y;
	
	Vector2D topLeft = (parentPosition + position);	
	Vector2D bottomRight = topLeft + size;
	
	printf("Checking click\n\ttopLeft=(%f,%f)\n\tbottomRight=(%f,%f)\n\tmouse=(%f,%f)\n",
		topLeft.x, topLeft.y, bottomRight.x, bottomRight.y, mousePosition.x, mousePosition.y);
	if(mousePosition.x >= topLeft.x && mousePosition.x <= bottomRight.x &&
	   mousePosition.y <= bottomRight.y && mousePosition.y >= topLeft.y)
	{
		
		if(scrollNeeded)
		{
			if(mousePosition.x >= topLeft.x + size.x - 2.5)
			{
				//in the bar
				if(mousePosition.y <= topLeft.y + 2.5)
				{
					//top arrow
					scrollIndex--;
					if(scrollIndex < 0)
						scrollIndex = 0;
				}
				else if(mousePosition.y >= bottomRight.y - 2.5)
				{
					//bottom arrow
					scrollIndex++;
					if(scrollIndex + listLines + 1 > stringList->GetLength())
						scrollIndex = stringList->GetLength() - listLines - 1;
					
				}
				return true;
			}	
		}
		int index = (mousePosition.y - topLeft.y)/(textSize.x * 1.5);
		char * string = stringList->GetString(index + scrollIndex);
		if(string)
		{
			isSelected = true;
			selectedElement = string;
			selectedIndex = index + scrollIndex;
			(gameManager->*clickStringAction)(string);
			printf("selected %s\n", selectedElement);
			return true;
		}
	}	
	return false;
}
